<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <form action="/user/login" method="post">
        <table>
            <tr>
                <td>
                    <input type="text" name="email" id="email" placeholder="邮箱"/>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="password" name="password" id="password" placeholder="密码"/>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="text" name="username" id="username" placeholder="用户名"/>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="vcode" name="vcode" id="vcode" placeholder="验证码"/>
                    <button type="button" onclick="sendMessage()" id="sendCode" >获取验证码</button>
                </td>
            </tr>
        </table>
<!--        <input type="submit" value="登录">-->
        <button type="button" onclick="toResgister()">注册</button>
    </form>

<!--    <script type="text/javascript" src="/js/jquery-2.1.0.js" ></script>-->
<!--    <script type="text/javascript" src="/js/bootstrap.js"></script>-->
<!--    <script type="text/javascript" src="/js/main.js"></script>-->
    <script type="text/javascript">

        function sendMessage()
        {
            var emailAd=document.getElementById("email");
            if(emailAd.value=="")
            {
                alert("邮箱不能为空");
                return ;
            }
            var email_reg=/^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
            if(!email_reg.test(emailAd.value))
            {
                alert("请输入正确的邮箱地址");
                return ;
            }
            $.get("/user/vcode",{"email":emailAd.value},function (data) {
                if(data)
                {
                    var wait = 60;
                    var button=document.getElementById("sendCode");
                    (function time(o) {
                        if (wait == 0) {
                            o.disabled=false;
                            o.innerHTML = "获取验证码";
                            wait = 60;
                        } else {
                            o.disabled=true;
                            o.innerHTML = wait + "秒后可以重新发送";
                            wait--;
                            setTimeout(function() {
                                time(o)
                            }, 1000)
                        }
                    })(button);
                }
            });
        }

        function toResgister()
        {
            var email=document.getElementById("email");
            var password=document.getElementById("password");
            var username=document.getElementById("username")
            var vcode=document.getElementById("vcode");

            if(vcode.value=="")
            {
                alert("验证码不能为空");
                return ;
            }
            $.post("/user/register",{"email":email.value,"password":password.value,"username":username.value,"vcode":vcode.value},function (data) {
                if (data=="SECCUSS"){
                    window.location.href = "/index.html";
                }
                else if(data=="DUPLICATE"){
                    alert("已经注册！")
                }
                else {
                    alert("验证码错误！")
                }
            });
            // $.get("/user/register",{"email":email.value,"password":password.value,"username":username.value,"vcode":vcode.value},function (data) {
            //     if (data){
            //         window.location.href = "/index";
            //     }
            // });

            // $.ajax({
            //     type: "POST",
            //     url: "/user/register",
            //     data: {"email":email.value,"password":password.value,"username":username.value,"vcode":vcode.value},
            //     dataType: "json",
            //     success:
            //     errer:
            // });
        }
    </script>
</body>
</html>